package cn.tedu.boot09.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author liyang
 * @version 1.0
 * @package cn.tedu.boot09.controller
 * @file UploadController.java
 * @description
 * @time 2024/7/30 下午4:20
 */
//获取真正的名字，获取真正的路径，写进文件
@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName= picFile.getOriginalFilename();
        System.out.println("文件名："+fileName);

        String suffix = fileName.substring(fileName.lastIndexOf("."));

        fileName = UUID.randomUUID()+suffix;
        System.out.println("唯一文件名："+fileName);
//        现设路径，再装进文件对象
        String dirPath = "D:/jsd0903files";
        File dirfile = new File(dirPath);
        if (!dirfile.exists()){
            dirfile.mkdirs();
        }
//        真正的路径：D:/jsd0903files/xxx.jpg
        String filePath = dirPath + "/" + fileName;
//        a.jpg➡D:/jsd0903files/xxx.jpg
        picFile.transferTo(new File(filePath));

        return fileName;
    }

    @RequestMapping("/remove")
    public void remove(String name){
        new File("d:/jsd0903files/"+name).delete();


    }
}
